定义:
将抽象部分和它的实现部分分离,使它们可以独立的变化。
类图:
类型:结构型
实例:
两种品牌的手机,运行相同的软件
C++ 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include <iostream> #include <string> using namespace std;
class HandsetSoft { public: virtual void run()=0; };
class HandsetGame:public HandsetSoft { public: void run() { cout<<"运行手机游戏"<<endl; } };
class HandsetAddressList:public HandsetSoft { public: void run() { cout<<"运行手机通讯录"<<endl; } };
class HandsetBrand { protected: HandsetSoft *soft; public: void setHandsetSoft(HandsetSoft *soft) { this->soft=soft; } virtual void run()=0; };
class HandsetBrandN:public HandsetBrand { public: void run() { cout<<"手机品牌N:"; soft->run(); } };
class HandsetBrandM:public HandsetBrand { public: void run() { cout<<"手机品牌M:"; soft->run(); } };
int main() { HandsetBrand *hb; hb=new HandsetBrandN(); hb->setHandsetSoft(new HandsetGame()); hb->run(); hb->setHandsetSoft(new HandsetAddressList()); hb->run();
hb=new HandsetBrandM(); hb->setHandsetSoft(new HandsetGame()); hb->run(); hb->setHandsetSoft(new HandsetAddressList()); hb->run();
return 0; }
|
Python 实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
class HandsetSoft(object): def Run(self): pass
class HandsetGame(HandsetSoft): def Run(self): print u"运行手机游戏"
class HandsetAddressList(HandsetSoft): def Run(self): print u"运行手机通讯录"
class HandsetBrand(object): def __init__(self): self.m_soft = None def SetHandsetSoft(self,temp): self.m_soft= temp def Run(self): pass
class HandsetBrandN(HandsetBrand): def Run(self): if not (self.m_soft == None): print u"手机品牌N:", self.m_soft.Run()
class HandsetBrandM(HandsetBrand): def Run(self): if not (self.m_soft == None): print u"手机品牌M:", self.m_soft.Run()
if __name__ == "__main__": brand = HandsetBrandN() brand.SetHandsetSoft(HandsetGame()) brand.Run() brand.SetHandsetSoft(HandsetAddressList()) brand.Run()
brand = HandsetBrandM() brand.SetHandsetSoft(HandsetGame()) brand.Run() brand.SetHandsetSoft(HandsetAddressList()) brand.Run()
|
执行结果:
1 2 3 4
| 手机品牌N: 运行手机游戏 手机品牌N: 运行手机通讯录 手机品牌M: 运行手机游戏 手机品牌M: 运行手机通讯录
|
Tips:
桥接模式
的Python
实现没有什么特点,在这就不在赘述了。